package com.example.cd5160866.urlapp.view;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewTreeObserver;


/**
 * 这个类可以帮你画线~
 */
public class LineDrawable extends Drawable {

    public static final int HORIZONTAL = 0;

    public static final int VERTICAL = 1;

    private int model = HORIZONTAL;

    private Paint mPaint;

    private int mColor;

    private int mSize = 0;

    private int mLength = 0;

    private int mDashWidth = 0;//虚线宽度

    private int mDashGap = 0;//虚线间隙

    private int padding = 0;

    /**
     * @param model  HORIZONTAL VERTICAL 横向或者竖向
     * @param color  线颜色
     * @param size   线粗细
     * @param length 线总长度
     */
    public LineDrawable(int model, int color, int size, int length) {
        this.model = model;
        this.mColor = color;
        this.mSize = size;
        this.mLength = length;
        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
    }

    /**
     * @param model     HORIZONTAL VERTICAL 横向或者竖向
     * @param color     线颜色
     * @param size      线粗细
     * @param length    线总长度
     * @param dashWidth 虚线宽度
     * @param dashGap   虚线间隙
     */
    public LineDrawable(int model, int color, int size, int length, int dashWidth, int dashGap) {
        this.model = model;
        this.mColor = color;
        this.mSize = size;
        this.mLength = length;
        this.mDashWidth = dashWidth;
        this.mDashGap = dashGap;
        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
    }

    public LineDrawable(int model, int color, View attachTargetView) {
        this.model = model;
        this.mColor = color;
        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
        attachToViewBackGround(attachTargetView);
    }

    /**
     * @param model            HORIZONTAL VERTICAL 横向或者竖向
     * @param color            线颜色
     * @param dashWidth        虚线宽度
     * @param dashGap          虚线间隙
     * @param attachTargetView 被设置为背景的view
     */
    public LineDrawable(int model, int color, int dashWidth, int dashGap, View attachTargetView) {
        this.model = model;
        this.mColor = color;
        this.mDashWidth = dashWidth;
        this.mDashGap = dashGap;
        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
        attachToViewBackGround(attachTargetView);
    }

    public int getPadding() {
        return padding;
    }

    public void setPadding(int padding) {
        this.padding = padding;
        invalidateSelf();
    }

    /**
     * 设置为虚线
     */
    public LineDrawable setDash(int dashWidth, int dashGap) {
        this.mDashWidth = dashWidth;
        this.mDashGap = dashGap;
        invalidateSelf();
        return this;
    }

    /**
     * 设置为view的背景
     */
    private LineDrawable attachToViewBackGround(final View view) {
        if (view.getWidth() == 0 || view.getHeight() == 0)
            view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mLength = model == HORIZONTAL ? view.getWidth() : view.getHeight();
                    mSize = model == HORIZONTAL ? view.getHeight() : view.getWidth();
                    invalidateSelf();
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                    DevUtil.d("qw", "绘制完成 " + mLength + " len" + mSize + " size");
                    view.setBackground(LineDrawable.this);
                }
            });
        else {
            mLength = model == HORIZONTAL ? view.getWidth() : view.getHeight();
            mSize = model == HORIZONTAL ? view.getHeight() : view.getWidth();
            invalidateSelf();
//            DevUtil.d("qw", "无需绘制 " + mLength + " len" + mSize + " size");
            view.setBackground(LineDrawable.this);
        }
        return this;
    }

    private void drawLine(Canvas canvas) {
        canvas.drawRect(0, 0, getIntrinsicWidth(), getIntrinsicHeight(), mPaint);
    }

    private void drawDashLine(Canvas canvas) {
        int start = 0;
        int loopCount = (mLength / mDashWidth) + 10;
        for (int i = 0; i < loopCount; i++) {
            if (model == VERTICAL) {
//                canvas.drawRect(0, start, mSize, start + mDashWidth, mPaint);
                int left = start + padding;
                if (left > mLength - padding)
                    left = mLength - padding - mDashWidth;
                int right = (start + mDashWidth + padding) > mLength ? (mLength - padding) : (start + mDashWidth + padding);
                if (right >= mLength)
                    right = mLength - padding;
                canvas.drawRect(0, left, mSize, right, mPaint);
            } else {
//                canvas.drawRect(start, 0, start + mDashWidth, mSize, mPaint);
                int top = start + padding;
                if (top > mLength - padding)
                    top = mLength - padding - mDashWidth;
                int bottom = (start + mDashWidth + padding) > mLength ? (mLength - padding) : (start + mDashWidth + padding);
                if (bottom >= mLength)
                    bottom = mLength - padding;
                canvas.drawRect(top, 0, bottom, mSize, mPaint);
            }
            start = start + mDashWidth + mDashGap;
        }
    }


    @Override
    public void draw(@NonNull Canvas canvas) {
        if (mDashWidth <= 0 || mDashGap <= 0)
            drawLine(canvas);
        else
            drawDashLine(canvas);
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int i) {
        mPaint.setAlpha(i);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getIntrinsicHeight() {
        if (model == VERTICAL)
            return mLength;
        return mSize;
    }

    @Override
    public int getIntrinsicWidth() {
        if (model == VERTICAL)
            return mSize;
        return mLength;
    }


    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}
